using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using ctsu.Admin.Forms;
using ctsu.BaseForms;
using ctsu.Security;
using ctsu.Shared;
////using Chb.ctsu.Data;

namespace ctsu.Admin.BusLogic
{
	/// <summary>
	/// Summary description for FormUsersBLImpl.
	/// </summary>
	public class BLFormUsersImpl : BLFormBaseImpl
	{
		public BLFormUsersImpl(FormMain formMain, FormBase form, FormInfoBase formInfo, DataTable tableSchema, Permission formSecurity, Hashtable configData)
			: base(formMain, form, formInfo, tableSchema, formSecurity, configData)
		{
		}

		public override bool Save()
		{
			bool retVal = base.Save();
			if (retVal)
			{
				Control ctl2 = Forms.Utils.GetControlByName("RePassword", this._form.Controls);
				ctl2.Text = "";
				ctl2.Enabled = false;
				MessageBox.Show("You must log off and log back on for the changes to take effect.", "Save Confirmation",
					MessageBoxButtons.OK, MessageBoxIcon.Information);
			}

			return retVal;
		}

		public override void Cancel()
		{
			base.Cancel();
			Control ctl2 = Forms.Utils.GetControlByName("RePassword", this._form.Controls);
			ctl2.Text = "";
			ctl2.Enabled = false;
		}

		private bool VerifyPassword(Control ctl)
		{
			FieldContext fc = (FieldContext)ctl.Tag;

			if (Forms.Utils.IsMissingValue(ctl))
			{
				if (fc.IsRequired)
				{
					fc.ErrorMessage = Constants.ValidErrorRequiredEmpty;
					return false;
				}
			}

			if (!fc.IsColumnChanged(fc.Column.ColumnName))
				return true;

			Control ctl2 = Forms.Utils.GetControlByName("RePassword", this._form.Controls);

			if (ctl.Text != ctl2.Text)
			{
				fc.ErrorMessage = "Password and re-write password does not match";
				return false;
			}
			return true;
		}

		public override void Central_TextChanged(object sender, EventArgs e)
		{
			base.Central_TextChanged(sender, e);
		}

		protected override void _vcManager_OnAfterHookEvents()
		{
			Control ctl2 = Forms.Utils.GetControlByName("RePassword", this._form.Controls);

			ctl2.TextChanged += new EventHandler(Central_TextChanged);
			ctl2.Validating += new CancelEventHandler(RePassword_Validating);

			//hook password text changed
			FieldContext fc = (FieldContext)this._fcManager.Item("Password");
			fc.Control.TextChanged += new EventHandler(Password_TextChanged);
			Console.WriteLine("_vcManager_OnAfterHookEvents override");
			base._vcManager_OnAfterHookEvents();
		}

		private void Password_TextChanged(object sender, EventArgs e)
		{
			if (this.FormMode == EditMode.Load)
				return;
			Control ctl2 = Forms.Utils.GetControlByName("RePassword", this._form.Controls);
			ctl2.Enabled = true;
		}

		private void RePassword_Validating(object sender, CancelEventArgs e)
		{
			FieldContext fc = (FieldContext)this._fcManager.Item("Password");
			bool b = this.VerifyPassword(fc.Control);
			this.CheckErrorStatus(b, fc);
		}
	}
}
